package com.jcl.recruit.resume.service;

import com.jcl.core.security.ShareUserToken;
import com.jcl.core.utils.BaseUtils;
import com.jcl.recruit.core.constants.ResumeLogType;
import com.jcl.recruit.core.web.utils.RequestUtils;
import com.jcl.recruit.log.logic.LoggerProxy;
import com.jcl.recruit.resume.logic.ResumeBasicInfoLogic;
import com.jcl.recruit.resume.logic.ResumeCommonLogic;
import com.jcl.recruit.resume.logic.ResumeTransferLogic;
import com.jcl.recruit.resume.logic.ResumeWorkLogic;
import com.jcl.recruit.resume.model.ResumeBasicInfo;
import com.jcl.recruit.resume.vo.ResumeResultEntity;
import com.jcl.recruit.resume.vo.ResumeSimpleResultEntity;
import com.jcl.recruit.searchresume.logic.SearchResumeWorkLogic;
import com.jcl.recruit.server.elastic.SearchResumeServer;
import com.jcl.recruit.server.elastic.vo.SearchResumeResultEntity;
import com.jcl.recruit.server.elastic.vo.SearchResumeSimpleResultEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName ResumeShareService
 * @Description TODO
 * @Author yyliu
 * @Date 2018/10/25 14:52
 * @Version 1.0
 **/
@Service
public class ResumeShareService {

    @Autowired
    protected ResumeTransferLogic resumeTransferLogic;

    @Autowired
    protected ResumeBasicInfoLogic resumeBasicInfoLogic;

    @Autowired
    private ResumeCommonLogic resumeCommonLogic;

    @Autowired
    protected SearchResumeWorkLogic searchResumeWorkLogic;

    @Autowired
    protected ResumeWorkLogic resumeWorkLogic;

    @Autowired
    private SearchResumeServer searchResumeServer;

    @Autowired
    private LoggerProxy loggerProxy;

    /**
     * @param id
     * @param fromType
     * @return
     */
    public Map<String, Object> getShareResume(Integer id, String fromType) {

        ShareEntity shareEntity = new ShareEntity();

        String corpCode = RequestUtils.getCorpCode();
        if (ShareUserToken.search_from.equals(fromType)) {
            SearchResumeSimpleResultEntity entity = searchResumeServer.findSimpleEntity(id);
            shareEntity.setSearchSimpleEntity(entity);
        } else {
            ResumeSimpleResultEntity entity = resumeCommonLogic.findSimpleEntity(corpCode, id);
            shareEntity.setResumeSimpleEntity(entity);
        }
        String url_token = ShareUserToken.encodeResumeToken(RequestUtils.getCorpCode(),
                RequestUtils.getUserId(), id.toString(), fromType);
        shareEntity.setUrlToken(url_token);

        return shareEntity.getShareResume();
    }

    /**
     * @param id
     * @param fromType
     * @return
     */
    public Map<String, Object> getShareResumeDetail(Integer id, String fromType) {

        ShareEntity shareEntity = new ShareEntity();

        String corpCode = RequestUtils.getCorpCode();
        if (ShareUserToken.search_from.equals(fromType)) {
            SearchResumeResultEntity entity = searchResumeServer.findEntity(id);
            shareEntity.setSearchEntity(entity);
        } else {
            ResumeResultEntity entity = resumeCommonLogic.findDetailEntity(corpCode, id);
            shareEntity.setResumeEntity(entity);
        }

        return shareEntity.getShareResumeDetail();
    }

    /**
     * @param id
     * @param fromType
     * @return
     */
    public int callbackShareResume(Integer id, String fromType) {

        String corpCode = RequestUtils.getCorpCode();
        ResumeBasicInfo resume = null;
        if (ShareUserToken.search_from.equals(fromType)) {
            resume = resumeBasicInfoLogic.findOneByExternalKey(corpCode, id.toString());
            if (null == resume) {
                try {
                    resume = resumeTransferLogic.transferResumeById(id, ResumeLogType.TYPE_ADD_SHARE, corpCode);
                    // 添加日志
                    loggerProxy.addResumeLog(resume.getId(), ResumeLogType.TYPE_ADD_SHARE);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        return 0;
    }

    public static class ShareEntity {

        private Map<String, Object> dataMap = null;

        public Map<String, Object> getShareResume(){
            if(null != searchSimpleEntity){
                dataMap = BaseUtils.convertToMap(searchSimpleEntity);
            }
            if(null != resumeSimpleEntity){
                dataMap = BaseUtils.convertToMap(resumeSimpleEntity);
            }
            if(null != dataMap){
                dataMap.put("url_token", urlToken);
            }
            return dataMap;
        }

        public Map<String, Object> getShareResumeDetail(){
            if(null != searchEntity){
                dataMap = BaseUtils.convertToMap(searchEntity);
            }
            if(null != resumeEntity){
                dataMap = BaseUtils.convertToMap(resumeEntity);
            }
            return dataMap;
        }

        String urlToken;
        SearchResumeSimpleResultEntity searchSimpleEntity;
        ResumeSimpleResultEntity resumeSimpleEntity;
        SearchResumeResultEntity searchEntity;
        ResumeResultEntity resumeEntity;

        public void setUrlToken(String urlToken) {
            this.urlToken = urlToken;
        }

        public void setSearchSimpleEntity(SearchResumeSimpleResultEntity searchSimpleEntity) {
            this.searchSimpleEntity = searchSimpleEntity;
        }

        public void setResumeSimpleEntity(ResumeSimpleResultEntity resumeSimpleEntity) {
            this.resumeSimpleEntity = resumeSimpleEntity;
        }

        public void setSearchEntity(SearchResumeResultEntity searchEntity) {
            this.searchEntity = searchEntity;
        }

        public void setResumeEntity(ResumeResultEntity resumeEntity) {
            this.resumeEntity = resumeEntity;
        }
    }

}
